/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package foxmob.mlts.controller.setter;

import foxmob.mlts.component.FileManager;
import foxmob.mlts.component.StringManager;
import foxmob.mlts.model.QuestionPaper;
import foxmob.mlts.model.TreeObject;
import foxmob.mlts.viewcontroller.setter.NewTestRecordForm;
import foxmob.mlts.viewcontroller.setter.QuestionForm;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Lunayo
 */
public class NewTestRecordFormController extends RootModifyFormController {
    
    private static final String EDIT_LABEL = "Save Record";
    private static final String ADD_LABEL = "Add Record";
    
    private NewTestRecordForm delegate;
    
    public NewTestRecordFormController(SetterFormController setterFormDelegate,
            NewTestRecordForm delegate) {
        super(setterFormDelegate);
        this.delegate = delegate;
    }
    
    public void setDelegate(NewTestRecordForm delegate) {
        this.delegate = delegate;
    }
    
    public NewTestRecordForm getDelegate() {
        return delegate;
    }
    
    public void eventChanged(SetterFormController.ActionEvent status) {
        if (this.currentState == SetterFormController.ActionEvent.ADD_EVENT) {
            delegate.getSaveButton().setText(ADD_LABEL);
            delegate.getAddSectionButton().setVisible(false);
            delegate.getDeleteButton().setVisible(false);
        } else if (this.currentState == SetterFormController.ActionEvent.EDIT_EVENT) {
            delegate.getSaveButton().setText(EDIT_LABEL);
            delegate.getAddSectionButton().setVisible(true);
            delegate.getDeleteButton().setVisible(true);
        } 
    }
    
    @Override
    public void setCurrentState(SetterFormController.ActionEvent state) {
        super.setCurrentState(state);
        eventChanged(state);
    }
    
    @Override
    public boolean validateInputComponent() {
        super.validateInputComponent();
        FileManager manager = FileManager.getInstanceObject();
        String message =  "Please input all of the information";
        if (delegate.getTestRecordText().getText().equals("") ||
                delegate.getTestRecordText().getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, message);
            delegate.getTestRecordText().requestFocus();
            return false;
        } else if (!StringManager.isValidName(delegate.getTestRecordText().getText())) {
            message = "Please choose another name";
            JOptionPane.showMessageDialog(null, message);
            delegate.getTestRecordText().requestFocus();
            return false;
        } else if (manager.isRecordExists(delegate.getTestRecordText().getText())) {
            message = "Please choose another name";
            JOptionPane.showMessageDialog(null, message);
            delegate.getTestRecordText().requestFocus();
            return false;
        }
        return true;
    }
    
    @Override
    public void resetInputComponent() {
        super.resetInputComponent();
        delegate.getDescriptionText().setText("");
        delegate.getTestRecordText().setText("");
    }
    
    public void fillTestRecordComponents(TreeObject obj) {
        delegate.getDescriptionText().setText(obj.getRecord().getDescription());
        delegate.getTestRecordText().setText(obj.getRecord().getTestName());
    }
    
    private QuestionPaper getTestRecordComponent() {
        QuestionPaper record = new QuestionPaper();

        record.setTestName(delegate.getTestRecordText().getText());

        return record;
    }
    
    private void editTestRecordFromRecord(QuestionPaper record) {
        try {
            // TODO add your handling code here:
            FileManager manager = FileManager.getInstanceObject();
            QuestionPaper componentRecord = getTestRecordComponent();
            record.setDescription(componentRecord.getDescription());
            record.setTestName(componentRecord.getTestName());
            // Save to file
            manager.saveToFile(record);
            String st ="Record saved successfully!";
            JOptionPane.showMessageDialog(null,st);
        } catch (IOException ex) {
            Logger.getLogger(QuestionForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private void addTestRecordToRecord() {
        if (!validateInputComponent()) return;
        try {
            // TODO add your handling code here:
            FileManager manager = FileManager.getInstanceObject();
            QuestionPaper record = getTestRecordComponent();
            // Save to file
            manager.saveToFile(record);
            String st ="Record added successfully!";
            JOptionPane.showMessageDialog(null,st);
        } catch (IOException ex) {
            Logger.getLogger(QuestionForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /*
     * Listener
     */
    
    public void saveButtonDidTap() {
        if (!validateInputComponent()) return;
        if (this.currentState == SetterFormController.ActionEvent.ADD_EVENT) {
            addTestRecordToRecord();
            // reset the component
            resetInputComponent();
            // show the setter
            setterFormDelegate.getSetterFormDelegate().setVisible(true);
        } else if (this.currentState == SetterFormController.ActionEvent.EDIT_EVENT) {
            editTestRecordFromRecord(treeObject.getRecord());
        }    
    }
    
    public void deleteButtonDidTap() {
        setterFormDelegate.invokeComponent(SetterFormController.ActionEvent.DELETE_EVENT);
    }
    
    public void addSectionButtonDidTap() {
        setterFormDelegate.invokeComponent(SetterFormController.ActionEvent.ADD_EVENT);
    }
    
}
